<?php
//图文文章
class cy_posts extends WP_Widget {

	function cy_posts() {
		$widget_ops = array('description' => '图文显示最热文章的插件');
		$this->WP_Widget('cy_posts', '&#xE000; 图文文章', $widget_ops);
	}

	function form($instance) {
		$title = isset($instance['title']) ? $instance['title'] : '推荐阅读';
		$days = isset($instance['days']) ? $instance['days'] : 90;
		$posts_num = isset($instance['posts_num']) ? $instance['posts_num'] : 10;
		$order = isset($instance['orderby']) ? $instance['orderby'] : '最新发布';
		$orderby = array('最新发布','评论数量','浏览次数');
?>
	<p>
		<label>标题：
			<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
		</label>
	</p>

	<p>
		<label>文章数量：
			<input id="<?php echo $this->get_field_id('posts_num'); ?>" name="<?php echo $this->get_field_name('posts_num'); ?>" type="text" value="<?php echo $posts_num; ?>" size="6" />
		</label>
	</p>

	<p>
		<label>统计天数：
			<input id="<?php echo $this->get_field_id('days'); ?>" name="<?php echo $this->get_field_name('days'); ?>" type="text" value="<?php echo $days; ?>" size="6" />
		</label>
	</p>

	<p>
		<label>排序方式：
			<select name="<?php echo $this->get_field_name('orderby'); ?>">
<?php
foreach ($orderby as $order){
	echo "				<option value=\"$order\"";
	if ($instance['orderby']==$order){
		echo 'selected="selected"';
	}
	echo ">$order</option>\n";
}?>
			</select>
		</label>
	</p>
<?php
	}

	function update($new_instance, $old_instance) { return $new_instance; }

	function widget($args, $instance) {
		extract( $args );
		$title = $instance['title'];
		$posts_num = $instance['posts_num'];
		$days = $instance['days'];
		$order = $instance['orderby'];
		echo $before_widget.$before_title.$title.$after_title;
		echo cy_posts($posts_num,$days,$order);
		echo $after_widget;
	}
}
register_widget('cy_posts');

function cy_posts($posts_num,$days,$order){
?> 
<ul class="cy_posts">
<?php 
global $wpdb;
if ($order == "浏览次数"){
$posts = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_type = 'post' AND TO_DAYS(now()) - TO_DAYS(post_date) < $days AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $posts_num");
}
else if ($order == "评论数量"){
$posts = $wpdb->get_results("SELECT ID , post_title , comment_count FROM $wpdb->posts WHERE post_type = 'post' AND TO_DAYS(now()) - TO_DAYS(post_date) < $days AND ($wpdb->posts.`post_status` = 'publish' OR $wpdb->posts.`post_status` = 'inherit') ORDER BY comment_count DESC LIMIT 0 , $posts_num ");
}
else { $posts = get_posts("numberposts=$posts_num"); }
global $post;
foreach($posts as $post){
?>
	<li>
<?php ini_img(48,48)?> 
		<a href="<?php echo get_permalink( $post->ID ); ?>"><?php echo $post->post_title?></a>
		<small class="date"><?php echo get_the_time('Y/m/d')?><?php if($order!=="最新发布") $order == "评论数量" ? print '&nbsp;|&nbsp;'.$post->comment_count.'条点评 ' : print '&nbsp;|&nbsp;'.$post->views.' ℃ '?></small>
	</li>
<?php }?>
<?php wp_reset_query()?>
</ul>
<?php
}
?>